#!/bin/bash

##########
# 需要安装imagemagick，brew install imagemagick
# 需要再根目录->assets->images下放置名称为ic_launcher.png的图片，会根据这个图片进行缩放各尺寸的icon，所以最好分辨率高点
##########

function parse_json(){
    echo "${1//\"/}" | sed "s/.*$2:\([^,}]*\).*/\1/"
}

# 设置pubspec.yaml中的name和version
function setPubspecConfig() {
  appName=$1
  versionName=$2
  versionCode=$3

  filename="pubspec.yaml"
  line_number=1
  count=0 # 只改2项，所以...
  while read rows
  do
    case "$rows" in
#      *"name:"* )
#        new_content="name: $appName"
#        sed -i "" "$line_number s/.*/$new_content/" $filename
#        count=$((count+1))
#        ;;
      *"version:"* )
        new_content="version: ${versionName}+$versionCode"
        sed -i "" "$line_number s/.*/$new_content/" $filename
        count=$((count+1))
        ;;
      *)
        ;;
    esac
    if [ $count = 2 ]; then # 只改2项，所以...
      break
    fi
    line_number=$(($line_number+1))
  done < $filename
}
# 设置Android的Icon
function setIconAndroid(){
  input_image="assets/images/ic_launcher.png" # 输入图片路径

  dirs=("android/app/src/main/res/mipmap-mdpi" "android/app/src/main/res/mipmap-hdpi" "android/app/src/main/res/mipmap-xhdpi" "android/app/src/main/res/mipmap-xxhdpi" "android/app/src/main/res/mipmap-xxxhdpi")
  sizes=(66 72 96 144 192)

  i=0
  while [ $i -lt ${#dirs[@]} ]
  do
      outputDir=${dirs[$i]}
      size=${sizes[$i]}

      if ! [ -d $outputDir ]; then
          mkdir $outputDir # 文件夹不存在
      fi

      output_image="$outputDir/ic_launcher.png" # 输出图片路径
      convert $input_image -resize "$sizex$size" $output_image
      ((i++))
  done
}
function setAndroidConfig(){
  appName=$1
  appId=$2
  minSdk=$3
  targetSdk=$4
  compileSdk=$5
  versionName=$6
  versionCode=$7
  signFilePath=$8
  signStorePassword=$9
  signKeyAlias=${10}
  signKeyPassword=${11}
  baseUrl=${12}
  baseUrlTest=${13}

  filename="android/gradle.properties"
  line_number=1
  new_content=""
  while read rows
  do
    case "$rows" in
      *"APP_NAME"* )
        new_content="APP_NAME=$appName"
        ;;
      *"APP_ID"* )
        new_content="APP_ID=$appId"
        ;;
      *"MIN_SDK"* )
        new_content="MIN_SDK=$minSdk"
        ;;
      *"TARGET_SDK"* )
        new_content="TARGET_SDK=$targetSdk"
        ;;
      *"COMPILE_SDK"* )
        new_content="COMPILE_SDK=$compileSdk"
        ;;
      *"APP_VERSION_NAME"* )
        new_content="APP_VERSION_NAME=$versionName"
        ;;
      *"APP_VERSION_CODE"* )
        new_content="APP_VERSION_CODE=$versionCode"
        ;;
      *"SIGN_FILE_PATH"* )
        new_content="SIGN_FILE_PATH=$signFilePath"
        ;;
      *"SIGN_STORE_PASSWORD"* )
        new_content="SIGN_STORE_PASSWORD=$signStorePassword"
        ;;
      *"SIGN_KEY_ALIAS"* )
        new_content="SIGN_KEY_ALIAS=$signKeyAlias"
        ;;
      *"SIGN_KEY_PASSWORD"* )
        new_content="SIGN_KEY_PASSWORD=$signKeyPassword"
        ;;
      *"BASE_URL"* )
        new_content="BASE_URL=$baseUrl"
        ;;
      *"TEST_URL"* )
        new_content="TEST_URL=$baseUrlTest"
        ;;

      *)
        new_content=""
        ;;
    esac

    if ! [ -z "$new_content" ]; then
#      sed -i "" "$line_number s/.*/$new_content/" $filename
      # 因为new_content中可能会有/，所以用#来分割
      sed -i "" "$line_number s#.*#$new_content#" $filename
    fi

    line_number=$(($line_number+1))
  done < $filename
}

function setIosName(){
  appName=$1

  filename="ios/Runner/Info.plist"

  line_number=1
  new_content=""
  while read rows
  do
    case "$rows" in
      *"<key>CFBundleDisplayName</key>"* )
        new_content="	<string>$appName</string>"
        sed -i "" "$(($line_number+1)) s#.*#$new_content#" $filename
        break
        ;;
      *)
        ;;
    esac

    line_number=$(($line_number+1))
  done < $filename
}
function setIosId(){
  appId=$1

  filename="ios/Runner/Info.plist"

  line_number=1
  new_content=""
  while read rows
  do
    case "$rows" in
      *"<key>CFBundleIdentifier</key>"* )
        new_content="	<string>$appId</string>"
        sed -i "" "$(($line_number+1)) s#.*#$new_content#" $filename
        break
        ;;
      *)
        ;;
    esac
    line_number=$(($line_number+1))
  done < $filename

  filename="ios/Runner.xcodeproj/project.pbxproj"
  line_number=0
  new_content=""
  while read rows
    do
      case "$rows" in
        *"PRODUCT_BUNDLE_IDENTIFIER"* )
          new_content="				PRODUCT_BUNDLE_IDENTIFIER = $appId;"
          sed -i "" "$(($line_number+1)) s#.*#$new_content#" $filename
#          break  # 不是一个，要删除
          ;;
        *)
          ;;
      esac
      line_number=$(($line_number+1))
    done < $filename
}
# 设置ios的Icon，需要安装imagemagick，brew install imagemagick
function setIconIos(){
  input_image="assets/images/ic_launcher.png" # 输入图片路径
  outputDir="ios/Runner/Assets.xcassets/AppIcon.appiconset"
  if ! [ -d $outputDir ]; then
      mkdir $outputDir # 文件夹不存在
  fi
  convert "$input_image" -resize "20x20" "$outputDir/Icon-App-20x20@1x.png"
  convert "$input_image" -resize "40x40" "$outputDir/Icon-App-20x20@2x.png"
  convert "$input_image" -resize "60x60" "$outputDir/Icon-App-20x20@3x.png"
  convert "$input_image" -resize "29x29" "$outputDir/Icon-App-29x29@1x.png"
  convert "$input_image" -resize "58x58" "$outputDir/Icon-App-29x29@2x.png"
  convert "$input_image" -resize "87x87" "$outputDir/Icon-App-29x29@3x.png"
  convert "$input_image" -resize "40x40" "$outputDir/Icon-App-40x40@1x.png"
  convert "$input_image" -resize "80x80" "$outputDir/Icon-App-40x40@2x.png"
  convert "$input_image" -resize "120x120" "$outputDir/Icon-App-40x40@3x.png"
  convert "$input_image" -resize "120x120" "$outputDir/Icon-App-60x60@2x.png"
  convert "$input_image" -resize "180x180" "$outputDir/Icon-App-60x60@3x.png"
  convert "$input_image" -resize "76x76" "$outputDir/Icon-App-76x76@1x.png"
  convert "$input_image" -resize "152x152" "$outputDir/Icon-App-76x76@2x.png"
  convert "$input_image" -resize "167x167" "$outputDir/Icon-App-83.5x83.5@2x.png"
  convert "$input_image" -resize "1024x1024" "$outputDir/Icon-App-1024x1024@1x.png"
}

function main(){
    cp config.json ./assets/

    #  读取配置文件
    json=`echo $(cat config.json) | sed s/[[:space:]]//g`

    appName=$(parse_json $json "app_name")
    versionName=$(parse_json $json "version_name")
    versionCode=$(parse_json $json "version_code")
    appId=$(parse_json $json "app_id")
    minSdk=$(parse_json $json "min_sdk")
    targetSdk=$(parse_json $json "target_sdk")
    compileSdk=$(parse_json $json "compile_sdk")
    signFilePath=$(parse_json $json "sign_file_path")
    signStorePassword=$(parse_json $json "sign_store_password")
    signKeyAlias=$(parse_json $json "sign_key_alias")
    signKeyPassword=$(parse_json $json "sign_key_password")
    baseUrl=$(parse_json $json "base_url")
    baseUrlTest=$(parse_json $json "base_url_test")

    # 设置pubspec.yaml中的name和version
    setPubspecConfig $appName $versionName $versionCode

    # 设置Android的icon
    setIconAndroid
    setAndroidConfig $appName $appId $minSdk $targetSdk $compileSdk $versionName $versionCode $signFilePath $signStorePassword $signKeyAlias $signKeyPassword $baseUrl $baseUrlTest

    # 设置iOS的图标
    setIconIos
    setIosName $appName
    setIosId $appId
}

main